﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Fodee.Controls;
using Fodee.BusinessComponent;
using Fodee.DataEntity;

namespace Fodee
{
    public partial class OpenTable : BaseForm
    {
        private string tableID = string.Empty;
        private string tableNum = string.Empty;
        private string tableName = string.Empty;
        private StaffBC sbc = new StaffBC();
        private PeopleSel ps = null;
        private System.Windows.Forms.Control currentContorl = null;
        public OpenTable()
        {
            InitializeComponent();
        }
        public OpenTable(string tableID, string tableName, string tableNum)
        {
            InitializeComponent();
            this.tableID = tableID;
            this.tableName = tableName;
            this.tableNum = tableNum;
        }

        private void OpenTable_Load(object sender, EventArgs e)
        {
            this.labTableName.Text = this.tableName + " [" + this.tableNum + "]";
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            TableBC tbc = new TableBC();
            FodeeDataSet table = tbc.GetById(int.Parse(this.tableID));
            table.fod_Table.Rows[0]["TableStatusID"] = 2;
            table.fod_Table.Rows[0]["TableStatusName"] = "已开台";
            tbc.Update(table);
            MainForm frm = (MainForm)this.Owner;
            frm.UpdateTableInfo();
            
            this.Close();
        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
        }

        void ps_OnClean(object sender, EventArgs e)
        {
            this.cbxWaiter.DataSource = null;
        }

        void ps_OnClean_Member(object sender, EventArgs e)
        {
            this.cbxCust.DataSource = null;
        }

        void ps_OnPersonSelected(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                string id = ((DataGridView)sender).Rows[e.RowIndex].Cells[0].Value.ToString();
                string name = ((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString();
                this.cbxWaiter.DataSource = null;
                DataTable dt = new DataTable();
                dt.Columns.Add("Id", typeof(int));
                dt.Columns.Add("Name", typeof(string));
                dt.Rows.Add(new object[] { id, name });

                this.cbxWaiter.DisplayMember = "Name";
                this.cbxWaiter.ValueMember = "Id";
                this.cbxWaiter.DataSource = dt;
                this.cbxWaiter.SelectedIndex = 0;
            }
        }

        void ps_OnPersonSelected_Member(object sender, DataGridViewCellEventArgs e)
        {
            string id = ((DataGridView)sender).Rows[e.RowIndex].Cells[0].Value.ToString();
            string name = ((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString();
            this.cbxCust.DataSource = null;
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Rows.Add(new object[] { id, name });

            this.cbxCust.DisplayMember = "Name";
            this.cbxCust.ValueMember = "Id";
            this.cbxCust.DataSource = dt;
            this.cbxCust.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbxCust_Click(object sender, EventArgs e)
        {
            if (ps != null)
                ps.Close();
            this.ShowPersonSelect(Fodee.Common.Constants.PersonSearchType.Member, string.Empty);
        }

        private void cbxWaiter_Click(object sender, EventArgs e)
        {
            if (ps != null)
                ps.Close();
            this.ShowPersonSelect(Fodee.Common.Constants.PersonSearchType.Staff, string.Empty);
        }

        private void ShowPersonSelect(Common.Constants.PersonSearchType type, string condition)
        {
            switch (type)
            {
                case Fodee.Common.Constants.PersonSearchType.Staff:
                    ps = new PeopleSel(Common.Constants.PersonSearchType.Staff);
                    ps.OnPersonSelected += new EventHandler<DataGridViewCellEventArgs>(ps_OnPersonSelected);
                    ps.OnClean += new EventHandler(ps_OnClean);                    
                    ps.Location = new Point(503, 392);
                    break;
                case Fodee.Common.Constants.PersonSearchType.Member:
                    ps = new PeopleSel(Common.Constants.PersonSearchType.Member);
                    ps.OnPersonSelected += new EventHandler<DataGridViewCellEventArgs>(ps_OnPersonSelected_Member);
                    ps.OnClean += new EventHandler(ps_OnClean_Member);                    
                    ps.Location = new Point(503, 365);
                    break;
            }
            ps.Show(this);
        }

        private void labTableName_MouseDown(object sender, MouseEventArgs e)
        {
            if(sender.GetType().Name!="ComboBox" && ps!=null)
                ps.Close();
        }

        private void CreateBillAndOrder(int tableID)
        {
            
        }

        private void OpenTable_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

    }
}